from alipay import AliPay
import os
from django3 import settings

private_key_path = os.path.join(settings.BASE_DIR, 'keys', 'rsa_private_key.pem')
public_key_path = os.path.join(settings.BASE_DIR, 'keys', 'ali_pub')
app_private_key_string = open(private_key_path).read()
alipay_public_key_string = open(public_key_path).read()




def get_order_url(order_num,order_amount,title='p1802',debug=False,**kwargs):
    if debug:
        appid = "2016091800541924"
    else:
        appid = "2018080660974232"

    alipay = AliPay(
        appid=appid,
        app_notify_url=None,  # 默认回调url
        app_private_key_string=app_private_key_string,
        alipay_public_key_string=alipay_public_key_string,  # 支付宝的公钥，验证支付宝回传消息使用，不是你自己的公钥,
        sign_type="RSA2", # RSA 或者 RSA2
        debug=debug  # 默认False
    )

    order_string = alipay.api_alipay_trade_page_pay(
        out_trade_no=order_num,
        total_amount=order_amount,
        subject=title,
        return_url="http://127.0.0.1:8004/docs/",
        notify_url="http://176.122.175.246:8004/orders/ali/notify" # 可选, 不填则使用默认notify url
    )



    online_url = "https://openapi.alipay.com/gateway.do?"
    dev_url = "https://openapi.alipaydev.com/gateway.do?"
    if debug:
        url = dev_url + order_string
        return url
    url = online_url + order_string
    return url